package com.zmn.oms.business.impl.conf.distribute;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightBService;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightListBService;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightPlatBService;
import com.zmn.oms.business.interfaces.conf.distribute.RobOrderConfigBService;
import com.zmn.oms.common.constant.ConfDistributeConsts;
import com.zmn.oms.common.constant.ConfDistributeWayEnum;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.common.dro.conf.distribute.*;
import com.zmn.oms.model.dio.conf.distribute.DistributeProductGroupDIO;
import com.zmn.oms.model.entity.conf.distribute.*;
import com.zmn.oms.model.vo.conf.distribute.DistributeWeightVO;
import com.zmn.oms.services.interfaces.conf.distribute.DistributeWeightRelationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 类描述：派单配置业务服务
 *
 * @author liuying
 * @date 2022/02/16 10:48
 */
@Slf4j
@Service
public class DistributeWeightListBServiceImpl implements DistributeWeightListBService {

    private final int SECOND_OF_HOURS = 60 * 30;
    @Resource
    private DistributeWeightBService distributeWeightBService;
    @Resource
    private DistributeWeightPlatBService distributeWeightPlatBService;
    @Resource
    private DistributeWeightRelationService distributeWeightRelationService;
    @Resource
    private RobOrderConfigBService robOrderConfigBService;
    @Resource
    private RedisManager redisManager;

    @Override
    public DistributeArgsDRO findDistributeArgsById(Integer plat, Integer companyId, Integer cityId, Integer bizType) {
        DistributeWeightVO vo = distributeWeightBService.findDistributeWeightById(plat, companyId, cityId, bizType);
        if (Objects.isNull(vo)) {
            log.debug("#distributeWeightBService#findDistributeArgsById#未查询到派单参数信息。plat:{}, companyId:{}, cityId:{}", plat, companyId, cityId);
            return null;
        }
        DistributeArgsDRO dro = BeanMapper.map(vo, DistributeArgsDRO.class);
        distributeArgsParameterAssembly(dro, vo.getDistributeApplication(), vo.getDistributeConfigId());
        return dro;
    }


    @Override
    public DistributeConfigDRO findDistributeConfig(Integer plat, Integer companyId, Integer cityId, Integer bizType) {
        DistributeWeightQuery query = DistributeWeightQuery.builder().plat(plat)
                .companyId(companyId)
                .cityId(cityId)
                .bizType(bizType)
                .productGroupId(null).build();
        return this.findDistributeConfig(query);
    }

    @Override
    public DistributeConfigDRO findDistributeConfig(DistributeWeightQuery distributeWeightQuery) {
        log.info("派单参数入参 == {}", JSON.toJSONString(distributeWeightQuery));
        Integer plat = distributeWeightQuery.getPlat();
        Integer companyId = distributeWeightQuery.getCompanyId();
        Integer cityId = distributeWeightQuery.getCityId();
        Integer bizType = distributeWeightQuery.getBizType();
        Integer productGroupId = distributeWeightQuery.getProductGroupId();
        // 获取平台直派配置
        DistributeWeightPlat distributeWeightPlat = distributeWeightPlatBService.findPlatDirectlyConfigByPlatAndBizTypeAndCityId(plat, bizType, cityId);
        // 计算是否应该平台直派
        boolean isPlatDirectly = Objects.nonNull(distributeWeightPlat);
        ConfDistributeWayEnum distributeWay;
        PlatDirectlyDistributeArgsDRO platDirectlyDistributeArgs;
        DistributeArgsDRO distributeArgs = null;
        DistributeProductGroupDIO productDIO = new DistributeProductGroupDIO();
        productDIO.setProductGroupId(productGroupId);
        if (isPlatDirectly) {
            platDirectlyDistributeArgs = new PlatDirectlyDistributeArgsDRO();
            BeanUtils.copyProperties(distributeWeightPlat, platDirectlyDistributeArgs);
            distributeWay = ConfDistributeWayEnum.DISTRIBUTE_WAY_PLAT;
            distributeArgsParameterAssembly(platDirectlyDistributeArgs, ConfDistributeConsts.DISTRIBUTE_APPLICATION_PLAT, distributeWeightPlat.getDistributePlatId());

            // 处理产品组查询对应的数据
            BeanUtils.copyProperties(distributeWeightPlat, productDIO);
            this.distributeProductGroupAssembly(productDIO, platDirectlyDistributeArgs, isPlatDirectly);

            // 分级派单参数
            platDirectlyDistributeArgs.setEngineerLevelStatus(distributeWeightPlat.getEngineerLevelStatus());
            platDirectlyDistributeArgs.setEngineerLevelList(distributeWeightPlat.getEngineerLevelList());
            platDirectlyDistributeArgs.setEngineerLevelGroupStatus(distributeWeightPlat.getEngineerLevelGroupStatus());

            // 默认设置为空 防止copy赋值问题
            platDirectlyDistributeArgs.setEngineerLevelGroupList(null);
            // 查询当前周期分组参数
            if (CollectionUtils.isNotEmpty(distributeWeightPlat.getEngineerLevelGroupListCurrent())) {
                List<DistributeEngineerLevelGroupDRO> list = Lists.newArrayListWithExpectedSize(distributeWeightPlat.getEngineerLevelGroupListCurrent().size());
                for (DistributeWeightEngineerLevelGroup distributeWeightEngineerLevelGroup : distributeWeightPlat.getEngineerLevelGroupListCurrent()) {
                    list.add(DistributeEngineerLevelGroupDRO.builder()
                            .priority(distributeWeightEngineerLevelGroup.getPriority())
                            .protectionCount(distributeWeightEngineerLevelGroup.getProtectionCount())
                            .currentWeekProtectionCountMap(distributeWeightEngineerLevelGroup.getCurrentWeekProtectionCountMap())
                            .engineerLevelList(
                                    Arrays.stream(Optional.ofNullable(distributeWeightEngineerLevelGroup.getEngineerLevels()).orElse("")
                                            .split(",")).map(Integer::parseInt).collect(Collectors.toList())).build()
                    );
                }
                platDirectlyDistributeArgs.setEngineerLevelGroupList(list);
            }
        } else {
            platDirectlyDistributeArgs = null;
            distributeWay = ConfDistributeWayEnum.DISTRIBUTE_WAY_SP;
            // 获取服务商派单配置
            log.debug("#distributeWeightBService#findDistributeWeightById#获取服务商派单配置reqParam,plat:{}, companyId:{}, cityId:{},bizType:{}", plat, companyId, cityId, bizType);
            DistributeWeightVO distributeWeightVO = distributeWeightBService.findDistributeWeightById(plat, companyId, cityId, bizType);
            log.debug("#distributeWeightBService#findDistributeWeightById#获取服务商派单配置respParam:{}", JSONObject.toJSON(distributeWeightVO));
            distributeArgs = BeanMapper.map(distributeWeightVO, DistributeArgsDRO.class);
            Integer distributeApplication = Objects.nonNull(distributeWeightVO) ? distributeWeightVO.getDistributeApplication() : null;
            Integer distributeConfigId = Objects.nonNull(distributeWeightVO) ? distributeWeightVO.getDistributeConfigId() : null;
            distributeArgsParameterAssembly(distributeArgs, distributeApplication, distributeConfigId);

            // 处理产品组查询对应的数据
            BeanUtils.copyProperties(distributeWeightVO, productDIO);
            this.distributeProductGroupAssembly(productDIO, distributeArgs, isPlatDirectly);
        }
        log.info("派单参数出参-End == {} == {}", JSON.toJSONString(platDirectlyDistributeArgs), JSON.toJSONString(distributeArgs));
        return new DistributeConfigDRO(platDirectlyDistributeArgs, distributeArgs, distributeWay.getCode());
    }

    @Override
    public RobOrderDRO findRobOrderConfig(RobOrderQuery query) {
        RobOrderDRO robOrderDRO = new RobOrderDRO();
        String redisKey = query.getCityId() + ":" + query.getCompanyId();
        //先去缓存查，有则返回，无则查询
        String redisData = redisManager.get(String.format(RedisKeyConsts.OMS_CONF_DISTRIBUTE_ROBORDERCONFIG, redisKey));
        if (StringUtils.isNotBlank(redisData)) {
            robOrderDRO = JSONObject.parseObject(redisData, RobOrderDRO.class);
            return robOrderDRO;
        }

        ConfDistributeWayEnum distributeWay;
        PlatDirectlyConfigDRO platDirectlyConfigDRO = new PlatDirectlyConfigDRO();
        CompanyConfigDRO companyConfigDRO = new CompanyConfigDRO();
        //平台直派
        if (ConfDistributeConsts.DISTRIBUTE_PLAT_DIRECTLY.equals(query.getDistributeWay())) {
            distributeWay = ConfDistributeWayEnum.DISTRIBUTE_WAY_PLAT;
            RobOrderConfigInfo robOrderConfigInfo = robOrderConfigBService.findRobOrderConfigByQuery(query);
            if (Objects.nonNull(robOrderConfigInfo)) {
                List<RobOrderRelationDRO> resultList = getRelationData(robOrderConfigInfo);
                BeanUtils.copyProperties(robOrderConfigInfo, platDirectlyConfigDRO);
                platDirectlyConfigDRO.setRobOrderRelationList(resultList);
                robOrderDRO.setDistributeWay(distributeWay.getCode());
                robOrderDRO.setPlatDirectlyConfigDRO(platDirectlyConfigDRO);
            }
        }
        if (ConfDistributeConsts.DISTRIBUTE_SERVICE_PROVIDER.equals(query.getDistributeWay())) {
            query.setDistributeWay(null);
            RobOrderConfigInfo robOrderCompanyConfig = robOrderConfigBService.findCompanyRobOrderConfigByQuery(query);
            //取公司配置，没有取平台配置
            if (Objects.nonNull(robOrderCompanyConfig)) {
                List<RobOrderRelationDRO> resultList = getRelationData(robOrderCompanyConfig);
                BeanUtils.copyProperties(robOrderCompanyConfig, companyConfigDRO);
                companyConfigDRO.setRobOrderRelationList(resultList);

                robOrderDRO.setDistributeWay(ConfDistributeConsts.DISTRIBUTE_SERVICE_PROVIDER);
                robOrderDRO.setCompanyConfigDRO(companyConfigDRO);
            } else {
                //查服务商平台的数据，cityId改为null
                query.setCityId(null);
                query.setCompanyId(null);
                redisKey = query.getCityId() + ":" + query.getCompanyId();
                query.setDistributeWay(ConfDistributeConsts.DISTRIBUTE_SERVICE_PROVIDER);
                String redisData1 = redisManager.get(String.format(RedisKeyConsts.OMS_CONF_DISTRIBUTE_ROBORDERCONFIG, redisKey));
                if (StringUtils.isNotBlank(redisData1)) {
                    robOrderDRO = JSONObject.parseObject(redisData1, RobOrderDRO.class);
                    return robOrderDRO;
                }
                RobOrderConfigInfo robOrderPlatConfig = robOrderConfigBService.findRobOrderConfigByQuery(query);
                if (Objects.nonNull(robOrderPlatConfig)) {
                    List<RobOrderRelationDRO> resultList = getRelationData(robOrderPlatConfig);
                    BeanUtils.copyProperties(robOrderPlatConfig, companyConfigDRO);
                    companyConfigDRO.setRobOrderRelationList(resultList);
                    robOrderDRO.setDistributeWay(ConfDistributeConsts.DISTRIBUTE_SERVICE_PROVIDER);
                }
                robOrderDRO.setCompanyConfigDRO(companyConfigDRO);
            }
        }
        //查询成功把配置数据写入缓存
        redisManager.setex(String.format(RedisKeyConsts.OMS_CONF_DISTRIBUTE_ROBORDERCONFIG, redisKey), JSONObject.toJSONString(robOrderDRO), SECOND_OF_HOURS);
        return robOrderDRO;
    }

    /**
     * 派单参数配置配置数据组装
     *
     * @param distributeArgs        派单参数配置配置
     * @param distributeApplication 派单应用：1.平台， 2.公司
     * @param distributeConfigId    派单配置id(派单应用为平台时取平台派单权重配置id，为公司时取公司派单权重配置id)
     * @author xujie
     * @since 2021-06-08
     */
    private void distributeArgsParameterAssembly(DistributeArgsDRO distributeArgs, Integer distributeApplication, Integer distributeConfigId) {

        if (distributeArgs == null) {
            return;
        }
        List<DistributeWeightRelation> weightRelationList = distributeWeightRelationData(distributeApplication, distributeConfigId);
        // 直营
        Optional<DistributeWeightRelation> optional = weightRelationList.stream().filter(w -> ConfDistributeConsts.WEIGHT_TYPE_DIRECTLY.equals(w.getWeightType())).findFirst();
        // 非直营
        Optional<DistributeWeightRelation> optionalNon = weightRelationList.stream().filter(w -> ConfDistributeConsts.WEIGHT_TYPE_DIRECTLY_NON.equals(w.getWeightType())).findFirst();
        if (optional.isPresent()) {
            DistributeWeightRelation weightRelation = optional.get();
            distributeArgs.setDistanceWeight(weightRelation.getDistanceWeight());
            distributeArgs.setHandlerWeight(weightRelation.getHandlerWeight());
            distributeArgs.setDaySuccessWeight(weightRelation.getDaySuccessWeight());
            distributeArgs.setVisitRateWeight(weightRelation.getVisitRateWeight());
            distributeArgs.setSuccessRateWeight(weightRelation.getSuccessRateWeight());
            distributeArgs.setAvtAmountWeight(weightRelation.getAtvAmountWeight());
            distributeArgs.setStarLevelScoreWeight(weightRelation.getStarLevelScoreWeight());
            distributeArgs.setDistributeNewCount(weightRelation.getDistributeNewCount());
            distributeArgs.setServeScore(weightRelation.getServeScore());
            distributeArgs.setDatumOrderHourRatio(weightRelation.getDatumOrderHourRatio());
        }
        if (optionalNon.isPresent()) {
            DistributeWeightRelation weightRelationNon = optionalNon.get();
            distributeArgs.setDistanceWeightNon(weightRelationNon.getDistanceWeight());
            distributeArgs.setHandlerWeightNon(weightRelationNon.getHandlerWeight());
            distributeArgs.setDaySuccessWeightNon(weightRelationNon.getDaySuccessWeight());
            distributeArgs.setVisitRateWeightNon(weightRelationNon.getVisitRateWeight());
            distributeArgs.setSuccessRateWeightNon(weightRelationNon.getSuccessRateWeight());
            distributeArgs.setAvtAmountWeightNon(weightRelationNon.getAtvAmountWeight());
            distributeArgs.setStarLevelScoreWeightNon(weightRelationNon.getStarLevelScoreWeight());
            distributeArgs.setDistributeNewCountNon(weightRelationNon.getDistributeNewCount());
            distributeArgs.setServeScoreNon(weightRelationNon.getServeScore());
            distributeArgs.setDatumOrderHourRatioNon(weightRelationNon.getDatumOrderHourRatio());
        }
    }

    /**
     * 工程师派单权重关联配置数据
     *
     * @param distributeApplication 派单应用：1.平台， 2.公司
     * @param distributeConfigId    派单配置id(派单应用为平台时取平台派单权重配置id，为公司时取公司派单权重配置id)
     * @return List<DistributeWeightRelation>
     * @author xujie
     * @since 2021-06-08
     */
    private List<DistributeWeightRelation> distributeWeightRelationData(Integer distributeApplication,
                                                                        Integer distributeConfigId) {

        DistributeWeightRelationQuery query = DistributeWeightRelationQuery.builder()
                .distributeConfigId(distributeConfigId)
                .distributeApplication(distributeApplication)
                .build();
        List<DistributeWeightRelation> distributeWeightRelationList = NumberUtil.isNullOrZero(distributeConfigId) ?
                new ArrayList<>() : distributeWeightRelationService.listByQuery(query);

        // 直营工程师权重默认数据
        DistributeWeightRelation distributeWeightRelation = DistributeWeightRelation
                .builder()
                .distanceWeight(40)
                .handlerWeight(30)
                .daySuccessWeight(30)
                .successRateWeight(0)
                .visitRateWeight(0)
                .atvAmountWeight(0)
                .starLevelScoreWeight(0)
                .weightType(ConfDistributeConsts.WEIGHT_TYPE_DIRECTLY)
                .distributeApplication(distributeApplication)
                .distributeConfigId(distributeConfigId)
                .build();
        // 非直营工程师权重默认数据
        DistributeWeightRelation distributeWeightRelationNon = DistributeWeightRelation
                .builder()
                .distanceWeight(40)
                .handlerWeight(30)
                .daySuccessWeight(30)
                .successRateWeight(0)
                .visitRateWeight(0)
                .atvAmountWeight(0)
                .starLevelScoreWeight(0)
                .weightType(ConfDistributeConsts.WEIGHT_TYPE_DIRECTLY_NON)
                .distributeApplication(distributeApplication)
                .distributeConfigId(distributeConfigId)
                .build();
        if (distributeWeightRelationList.size() == 0) {
            distributeWeightRelationList.add(distributeWeightRelation);
            distributeWeightRelationList.add(distributeWeightRelationNon);
        } else if (distributeWeightRelationList.size() == 1) {
            DistributeWeightRelation d = distributeWeightRelationList.get(0);
            if (ConfDistributeConsts.WEIGHT_TYPE_DIRECTLY.equals(d.getWeightType())) {
                distributeWeightRelationList.add(distributeWeightRelationNon);
            } else if (ConfDistributeConsts.WEIGHT_TYPE_DIRECTLY_NON.equals(d.getWeightType())) {
                distributeWeightRelationList.add(distributeWeightRelation);
            } else {
                distributeWeightRelationList.add(distributeWeightRelation);
                distributeWeightRelationList.add(distributeWeightRelationNon);
            }
        }
        return distributeWeightRelationList;
    }


    private List<RobOrderRelationDRO> getRelationData(RobOrderConfigInfo robOrderConfigInfo) {
        RobOrderRelationQuery robOrderRelationQuery = RobOrderRelationQuery.builder()
                .configId(robOrderConfigInfo.getConfigId())
                .build();
        List<RobOrderRelation> robOrderRelationList = robOrderConfigBService.listByQuery(robOrderRelationQuery);
        List<RobOrderRelationDRO> resultList = new ArrayList<>();
        if (CollectionUtil.isNotNullOrEmpty(robOrderRelationList)) {
            resultList = robOrderRelationList.stream().map(rs -> {
                RobOrderRelationDRO dro = new RobOrderRelationDRO();
                BeanUtils.copyProperties(rs, dro);
                return dro;
            }).collect(Collectors.toList());
            resultList.forEach(item -> {
                RobOrderRelationProductQuery robOrderRelationProductQuery = RobOrderRelationProductQuery.builder()
                        .relationId(item.getRelationId())
                        .build();
                List<RobOrderRelationProduct> robOrderRelationProductList = robOrderConfigBService.listProductByQuery(robOrderRelationProductQuery);
                List<RobOrderRelationProductDRO> itemDROS = robOrderRelationProductList.stream().map(i -> {
                    RobOrderRelationProductDRO dro = new RobOrderRelationProductDRO();
                    BeanUtils.copyProperties(i, dro);
                    return dro;
                }).collect(Collectors.toList());
                item.setRobOrderRelationProductList(itemDROS);

            });
        }
        return resultList;
    }

    /**
     * 平台直派 && 服务商 产品组查询处理
     *
     * @param distributeWeightPlatDIO
     * @param distributeArgsDRO
     * @param isPlatDirectly true 平台直派
     */
    private void distributeProductGroupAssembly(DistributeProductGroupDIO distributeWeightPlatDIO,
                                                DistributeArgsDRO distributeArgsDRO,
                                                boolean isPlatDirectly) {
        // 处理产品组自动派单时间
        if (StringUtils.isNotBlank(distributeWeightPlatDIO.getAutoTimeProductGroupJson())) {
            Map<String, String> productGroupTimeMap = JSON.parseObject(distributeWeightPlatDIO.getAutoTimeProductGroupJson(), HashMap.class);
            String autoTime = productGroupTimeMap.get(String.valueOf(distributeWeightPlatDIO.getProductGroupId()));
            if (StringUtils.isNotBlank(autoTime)) {
                String[] timeStrArray = autoTime.split(" ~ ");
                distributeArgsDRO.setAutoTimeStart(timeStrArray[0]);
                distributeArgsDRO.setAutoTimeEnd(timeStrArray[1]);
            }
        }
        // 没有找到产品对应的自动派单，使用默认时间 (这里可以不处理，调用的地方已经使用 mapper 赋值)
        if (Objects.isNull(distributeArgsDRO.getAutoTimeStart())) {
            distributeArgsDRO.setAutoTimeStart(distributeWeightPlatDIO.getAutoTimeStart());
            distributeArgsDRO.setAutoTimeEnd(distributeWeightPlatDIO.getAutoTimeEnd());
        }

        // 首轮保护次数
        if (StringUtils.isNotBlank(distributeWeightPlatDIO.getProtectionFirstProductGroupJson())) {
            Map<Integer, Integer> map = JSON.parseObject(distributeWeightPlatDIO.getProtectionFirstProductGroupJson(),
                    new TypeReference<Map<Integer, Integer>>() {});
            distributeArgsDRO.setProtectionFirstProductGroupMap(map);
        }

        // 处理多轮保护次数
        if (Objects.equals(distributeWeightPlatDIO.getAutoRecommendProtectionType(), ConfDistributeConsts.PROTECTION_MANY_RULE)) {

            // 多轮优先 - 产品组保护次数 - 工程师维度
            if (StringUtils.isNotBlank(distributeWeightPlatDIO.getPriorityProtectionTimesProductGroup())) {
                // 解析的时候做兼容处理，新的数据格式 是两层嵌套，Map<ProductGroupId, Map<EngineerLevel, Value>>
                // 新的json格式：{"1013":{"0":"3","1":"3","2":"3","3":"3","4":"3","5":"3","6":"3","7":"3","8":"3","9":"3","10":"3","100",1}}
                // 老的json格式：{"1103":"78","1045":"10","1024":"12"}
                if (distributeWeightPlatDIO.getPriorityProtectionTimesProductGroup().contains(":{")) {
                    Map<Integer, Map<Integer, Integer>> priorityProtectionTimesMap = JSON.parseObject(distributeWeightPlatDIO.getPriorityProtectionTimesProductGroup(),
                            new TypeReference<Map<Integer, Map<Integer, Integer>>>() {});
                    distributeArgsDRO.setPriorityProtectionTimesProductGroupMap(priorityProtectionTimesMap);
                } else {
                    // 多轮优先 - 产品组保护次数 - 兼容历史配置
                    Map<String, Integer> priorityProtectionTimesMap = JSON.parseObject(distributeWeightPlatDIO.getPriorityProtectionTimesProductGroup(),
                            new TypeReference<Map<String, Integer>>() {});
                    distributeArgsDRO.setPriorityProtectionGroupTimes(priorityProtectionTimesMap);
                }
            }

            // 多轮普通 - 产品组保护次数
            if (StringUtils.isNotBlank(distributeWeightPlatDIO.getProtectionTimesProductGroupJson())) {
                // 解析的时候做兼容处理，新的数据格式 是两层嵌套，Map<ProductGroupId, Map<EngineerLevel, Value>>
                // 新的json格式：{"1013":{"0":"3","1":"3","2":"3","3":"3","4":"3","5":"3","6":"3","7":"3","8":"3","9":"3","10":"3","100",1}}
                // 老的json格式：{"1103":"78","1045":"10","1024":"12"}
                if (distributeWeightPlatDIO.getProtectionTimesProductGroupJson().contains(":{")) {
                    Map<Integer, Map<Integer, Integer>> priorityProtectionTimesMap = JSON.parseObject(distributeWeightPlatDIO.getProtectionTimesProductGroupJson(),
                            new TypeReference<Map<Integer, Map<Integer, Integer>>>() {});
                    distributeArgsDRO.setProtectionTimesNormalProductGroupMap(priorityProtectionTimesMap);
                } else {
                    Map<String, String> protectionTimesMap = JSON.parseObject(distributeWeightPlatDIO.getProtectionTimesProductGroupJson(), HashMap.class);
                    Map<String, Integer> protectionGroupTime = new HashMap<>(protectionTimesMap.keySet().size());
                    protectionTimesMap.keySet().stream().forEach((key) -> {
                        Integer value = distributeWeightPlatDIO.getProtectionDefaultTimes();
                        if (StringUtils.isNotBlank(protectionTimesMap.get(key))) {
                            value = Integer.valueOf(protectionTimesMap.get(key));
                        }
                        protectionGroupTime.put(key, value);
                    });
                    distributeArgsDRO.setProtectionGroupTimes(protectionGroupTime);
                }
            }
        }
    }
}
